package com.example.huanxin.more;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

public class DownLoad {
	
	public Handler handler;
	
	public DownLoad (Handler handler){
		this.handler = handler;
	}
	
	private Executor threadPool = Executors.newFixedThreadPool(3); 
	
	static class DownLoadRunnable implements Runnable{
		
		private String url;
		private String fileName;
		private long start;
		private long end;
		private Handler handler;
		
		public DownLoadRunnable(String url, String fileName, long start,
				long end,Handler handler) {
			super();
			this.url = url;
			this.fileName = fileName;
			this.start = start;
			this.end = end;
			this.handler=handler;
		}

		@SuppressWarnings("resource")
		@Override
		public void run() {
			try {
				URL httpUrl = new URL(url);
				HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
				conn.setReadTimeout(5000);
				conn.setRequestProperty("Range", "bytes="+start+"-"+end);
				conn.setRequestMethod("GET");
				RandomAccessFile accessFile = new RandomAccessFile(new File(fileName), "rwd");
				accessFile.seek(start);
				InputStream inputStream = conn.getInputStream();
				byte[] b = new byte[1024*4];
				int len;
				while((len = inputStream.read(b)) != -1){
					accessFile.write(b, 0, len);
				}
				if(accessFile != null){
					accessFile.close();
				}
				if(inputStream != null){
					inputStream.close();
				}
				Message message = new Message();
				message.what = 1;
				handler.sendMessage(message);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public void downLoadFile(String url){
		try {
			URL httpUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
			conn.setReadTimeout(5000);
			conn.setRequestMethod("GET");
			int count = conn.getContentLength();
			int block=count/3;
			String fileName=getFileName(url);
			File parent=Environment.getExternalStorageDirectory();
			File fileDownLoad=new File(parent,fileName);
			
			for (int i = 0; i < 3; i++) {
				long start=i*block;
				long end=(i+1)*block-1;
				if(i==2){
					end=count;
				}
				DownLoadRunnable runnable=new DownLoadRunnable(url,fileDownLoad.getAbsolutePath(),start,end,handler);
				threadPool.execute(runnable);
			}
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getFileName(String url){
		return url.substring(url.lastIndexOf("/")+1)+".jpg";
	}
}
